import request from "@/utils/request";

const STRATEGY_BASE_URL = "/ig/datasource";

const IndicatorManageAPI = {
  /** 获取数据源 */
  DataSourceList() {
    return request<any, DataSourceList[]>({
      url: `${STRATEGY_BASE_URL}/list`,
      method: "get",
    });
  },

  /** 获取源表列表 */
  TableList(queryParams?: TableListParameter) {
    return request<any, string[]>({
      url: `${STRATEGY_BASE_URL}/list/table`,
      method: "get",
      params: queryParams,
    });
  },

  /** 获取表结构 */
  TableInfo(queryParams?: TableInfoParameter) {
    return request<any, TableInfoResponse>({
      url: `${STRATEGY_BASE_URL}/list/table/info`,
      method: "get",
      params: queryParams,
    });
  },

  /** 根据输入的SQL语句获取信息 */
  SQLParser(data?: SQLParserRequestParameter) {
    return request<any, SQLParserResponse>({
      url: `ig/indicator/sql/parser`,
      method: "post",
      data: data,
    });
  },
};

export default IndicatorManageAPI;

/** 指标管理分页查询参数 */
export interface IndicatorManagePageQuery extends PageQuery {}

export interface DataSourceList {
  id?: number;
  aliasName?: string;
  dataSourceName?: string;
}

export interface TableListParameter {
  id?: number;
}

export interface DataBasePageVO {
  /** 序号 */
  index?: number;
  /** 字段名称 */
  fieldName?: string;
  /** 字段类型 */
  fieldType?: string;
}

export interface TableInfoParameter {
  id?: number;
  table?: string;
}

export interface TableInfoResponse {
  schema?: string;
  catalog?: string;
  tableName?: string;
  comment?: string;
  pkNames?: string[];
  indexInfoList?: indexInfo[];
  columns?: FieldColumnsConfig[];
}

export interface indexInfo {
  nonUnique?: boolean;
  indexName?: string;
  tableName?: string;
  schema?: string;
  catalog?: string;
  columnIndexInfoList?: columnIndexInfo[];
}

export interface columnIndexInfo {
  columnName?: string;
  ascOrDesc?: string;
}

export interface FieldColumnsConfig {
  tableName?: string;
  name?: string;
  type?: number;
  typeName?: string;
  size?: number;
  digit?: number;
  comment?: string;
  autoIncrement?: boolean;
  columnDef?: string;
  nullable?: boolean;
  typeEnum?: string;
  pk?: boolean;
}

export interface SQLParserRequestParameter {
  dataSourceId?: number;
  sqlStr?: string;
}

export interface SQLParserResponse {
  columnMetadataList: ColumnMetadataList[];
  selectColumns: string[];
  tableMetadataMap: TableMetadataMap;
  tableNames: string[];
}

export interface ColumnMetadataList {
  columnName: string;
  columnSize: number;
  comment: string;
  dataType: number;
  decimalDigits: number;
  nullable: boolean;
  pk: boolean;
  typeName: string;
}

export interface TableMetadataMap {
  columns: Column[];
  comment: string;
  primaryKeys: string[];
  tableName: string;
}

export interface Column {
  columnName: string;
  columnSize: number;
  comment: string;
  dataType: number;
  decimalDigits: number;
  nullable: boolean;
  pk: boolean;
  typeName: string;
}
